package bg.btanev.eightyfour.client;

import bg.btanev.eightyfour.client.composite.BasicTabComposite;
import bg.btanev.eightyfour.client.events.TargetEvent;
import bg.btanev.eightyfour.client.events.TargetEventHandler;
import bg.btanev.eightyfour.client.presenter.BasePresenter;
import bg.btanev.eightyfour.client.presenter.Presenter;
import bg.btanev.eightyfour.client.presenter.TargetTabPresenter;
import bg.btanev.eightyfour.client.service.GWTCommandServiceAsync;
import bg.btanev.eightyfour.client.view.TargetTabView;

import com.google.gwt.event.logical.shared.HasBeforeSelectionHandlers;
import com.google.gwt.event.logical.shared.HasSelectionHandlers;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

/**
 *
 * @author B.Tanev
 *
 */
public class RootAppControler extends BasePresenter implements ValueChangeHandler<String> {

	public static interface RootTabDisplay extends Display {
		HasBeforeSelectionHandlers<Integer> getBeforeSelectionHandlers();

		HasSelectionHandlers<Integer> getSelectionHandlers();

		BasicTabComposite getTab(int pos);

		void addTab(BasicTabComposite tab);
	}

	private TargetTabView targetsTab;
	private HasWidgets mainContainer;

	@SuppressWarnings("unchecked")
	public RootAppControler(GWTCommandServiceAsync rpcService, HandlerManager eventBus, RootTabDisplay view) {
		super(view, eventBus, rpcService);
	}

	@Override
	protected void bind() {
		History.addValueChangeHandler(this);

		this.targetsTab = CreateTargetsTab();

		final RootTabDisplay display = getDisplay();
		display.addTab(targetsTab);

		display.getSelectionHandlers().addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				Integer pos = event.getSelectedItem();
				if (pos != null) {
					BasicTabComposite selectedTab = display.getTab(pos.intValue());
					if (selectedTab != null) {
						eventBus.fireEvent(new TargetEvent());
					}
				}
			}
		});

		eventBus.addHandler(TargetEvent.TYPE, new TargetEventHandler() {
			@Override
			public void onListTargets() {
				History.newItem(targetsTab.getTabIdentifier());
			}
		});
	}

	private TargetTabView CreateTargetsTab() {
		TargetTabView targetTab = new TargetTabView();

		targetTab.setTabCaption("Targets");
		targetTab.setTabIdentifier("targets");

		return targetTab;
	}

	@Override
	public void go(HasWidgets container) {
		this.mainContainer = container;

		container.clear();
		container.add(getDisplay().asWidget());

		if ("".equals(History.getToken())) {
			History.newItem(targetsTab.getTabIdentifier());
		} else {
			History.fireCurrentHistoryState();
		}
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String token = event.getValue();

		if (token != null) {
			HasWidgets container = mainContainer;
			Presenter presenter = null;

			if (token.equals(targetsTab.getTabIdentifier())) {
				presenter = new TargetTabPresenter(targetsTab, eventBus, rpcService);
				container = null;
			}

			if (presenter != null) {
				presenter.go(container);
			}
		}
	}
}
